package com.rainng.coursesystem.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rainng.coursesystem.dao.mapper.LeaderMapper;
import com.rainng.coursesystem.model.entity.LeaderEntity;
import com.rainng.coursesystem.model.vo.response.table.LeaderEventItemVO;
import com.rainng.coursesystem.model.vo.response.table.LeaderItemVO;
import com.rainng.coursesystem.model.vo.response.table.TimetableItemVO;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class LeaderDAO extends BaseDAO {
    public static final int PAGE_SIZE = 20;

    private final LeaderMapper mapper;

    public LeaderDAO(LeaderMapper mapper) {
        this.mapper = mapper;
    }

    public int insert(LeaderEntity entity) {
        return mapper.insert(entity);
    }

    public int delete(Integer id) {
        return mapper.deleteById(id);
    }

    public LeaderEntity get(Integer id) {
        return mapper.selectById(id);
    }

    public int update(LeaderEntity entity) {
        return mapper.updateById(entity);
    }

    public int count(String areaName, String name) { return mapper.count(areaName, name); }

    public List<LeaderItemVO> getPage(Integer index, String areaName, String name) {
        Page<LeaderItemVO> page = new Page<>(index, PAGE_SIZE);

        return mapper.getPage(page, areaName, name).getRecords();
    }

    public LeaderEntity getByNumber(String number) {
        LambdaQueryWrapper<LeaderEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LeaderEntity::getNumber, number);

        return mapper.selectOne(wrapper);
    }

    public Integer countByAreaId(Integer areaId) {
        LambdaQueryWrapper<LeaderEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LeaderEntity::getAreaId, areaId);

        return mapper.selectCount(wrapper);
    }

    public List<LeaderEntity> listName() {
        LambdaQueryWrapper<LeaderEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(LeaderEntity::getId, LeaderEntity::getName);

        return mapper.selectList(wrapper);
    }

    public List<TimetableItemVO> listLeaderTimetable(Integer leaderId) {
        return mapper.listLeaderTimetable(leaderId);
    }

    public List<LeaderEventItemVO> listLeaderEvent(Integer leaderId) {
        return mapper.listLeaderEvent(leaderId);
    }
}
